<template>
  <div class="categories">
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品分类</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card>
      <el-row>
        <el-col>
          <el-button type="primary" @click="showaddCateDialog"
            >添加分类</el-button
          >
        </el-col>
      </el-row>
      <!--表格-->
      <tree-table
        :data="cateList"
        :columns="columns"
        :selection-type="false"
        :expand-type="false"
        show-index
        index-text="#"
        border
      >
        <!--是否有效-->
        <template slot="isok" slot-scope="scope">
          <i
            class="el-icon-success"
            v-if="scope.row.cat_deleted === false"
            style="color: lightgreen"
          ></i>
          <i class="el-icon-error" v-else style="color: lightgreen"></i>
        </template>
        <!--排序-->
        <template slot="order" slot-scope="scope">
          <el-tag size="mini" v-if="scope.row.cat_level === 0">一级</el-tag>
          <el-tag
            type="success"
            size="mini"
            v-else-if="scope.row.cat_level === 1"
            >二级</el-tag
          >
          <el-tag type="warning" size="mini" v-else>三级</el-tag>
        </template>
        <!--操作-->
        <template slot="opt">
          <el-button type="primary" icon="el-icon-edit" size="mini"
            >编辑</el-button
          >
          <el-button type="danger" icon="el-icon-delete" size="mini"
            >删除</el-button
          >
        </template>
      </tree-table>
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryCateInfo.pagenum"
        :page-sizes="[2, 5, 7, 10]"
        :page-size="queryCateInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>
    <el-dialog
      title="提示"
      :visible.sync="addCateDialogVisible"
      width="50%"
      @close="addCateDialogClosed"
    >
      <el-form
        :model="addCateForm"
        :rules="addCateFormRules"
        ref="addCateFormRef"
        label-width="100px"
      >
        <el-form-item label="分类名称" prop="cat_name">
          <el-input v-model="addCateForm.cat_name"></el-input>
        </el-form-item>
        <el-form-item label="父级分类:">
          <el-cascader
            v-model="selscteddKeys"
            :options="parentCateList"
            :props="cascaderProps"
            @change="parentCateChange"
            clearable
          ></el-cascader>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addCateDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addCate">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { getCateListApi, addCateApi } from "@/api/user";
export default {
  name: "CategoriesIndex",
  data() {
    return {
      cateList: [],
      // 父级分类的列表
      parentCateList: [],
      // 查询条件
      queryCateInfo: {
        type: 3,
        pagenum: 1,
        pagesize: 2,
      },
      total: 0,
      addCateDialogVisible: false,
      addCateForm: {
        cat_name: "",
        cat_pid: 0,
        cat_level: 0,
      },
      selscteddKeys: [],
      cascaderProps: {
        checkStrictly: true,
        expandTrigger: "hover",
        value: "cat_id",
        label: "cat_name",
        children: "children",
      },
      addCateFormRules: {
        cat_name: [
          { required: true, message: "请输入分类的名称", trigger: "blur" },
        ],
      },
      columns: [
        {
          label: "分类名称",
          prop: "cat_name",
        },
        {
          label: "是否有效",
          //将当前列表页定义为模板页
          type: "template",
          //当前这一列使用模板名称
          template: "isok",
        },
        {
          label: "排序",
          //将当前列表页定义为模板页
          type: "template",
          //当前这一列使用模板名称
          template: "order",
        },
        {
          label: "操作",
          //将当前列表页定义为模板页
          type: "template",
          //当前这一列使用模板名称
          template: "opt",
        },
      ],
    };
  },
  methods: {
    async getCateList() {
      const res = await getCateListApi(this.queryCateInfo);
      // console.log(res);
      if (res.meta.status !== 200) {
        return this.$message.error("获取商品分类失败！");
      }
      // this.$message.success("获取商品分类成功！");
      this.cateList = res.data.result;
      this.total = res.data.total;
    },
    handleSizeChange(currentPage) {
      this.queryCateInfo.pagesize = currentPage;
      this.getCateList();
    },
    handleCurrentChange(currentPage) {
      this.queryCateInfo.pagenum = currentPage;
      this.getCateList();
    },
    showaddCateDialog() {
      this.addCateDialogVisible = true;
      this.getParentCateList();
    },
    async getParentCateList() {
      const res = await getCateListApi({
        type: 2,
      });
      // console.log(res);
      if (res.meta.status !== 200) {
        return this.$message.error("获取商品分类列表失败！");
      }
      this.parentCateList = res.data;
    },
    parentCateChange() {
      if (this.selscteddKeys.length > 0) {
        this.addCateForm.cat_pid =
          this.selscteddKeys[this.selscteddKeys.length - 1];
        this.addCateForm.cat_level = this.selscteddKeys.length;
        return;
      } else {
        this.addCateForm.cat_pid = 0;
        this.addCateForm.cat_level = 0;
      }
    },
    addCateDialogClosed() {
      this.$refs.addCateFormRef.resetFields();
      this.selscteddKeys = [];
      this.addCateForm.cat_level = 0;
      this.addCateForm.cat_pid = 0;
    },
    async addCate() {
      this.$refs.addCateFormRef.validate();
      const res = await addCateApi(this.addCateForm);
      if (res.meta.status !== 201) {
        return this.$message.error("添加分类失败！");
      }
      this.$message.success("添加分类成功！");
      this.getCateList();
      this.addCateDialogVisible = false;
    },
  },
  created() {
    this.getCateList();
  },
};
</script>

<style lang="scss" scoped>
.categories {
  .el-breadcrumb {
    margin-bottom: 15px;
  }
  .el-card {
    .el-row {
      margin-bottom: 15px;
    }
    .el-pagination {
      margin-top: 15px;
    }
  }
  .el-cascader {
    width: 100%;
  }
}
</style>
